/* START 1b_tripCountsByHour.do */

/* INCLUDED AS FILE IN 1_tripCountsByDayAroundOpening.do */

fv
frame put oa CCK gCCK tripDate hr TKT_TYP_CD, into(cckbyhour)
fv cckbyhour
preserve
gcollapse (count) n = oa, by(CCK tripDate hr)
gen byte after = (tripDate >= td(27dec2015))
gen logn = log(n)
gegen grp = group(CCK after)
binscatter2 logn hr, by(grp) linetype(none)

gegen nRef = mean(n) if tripDate == td(01dec2015), by(CCK)
fillmissing nRef, by(CCK)
gen nnorm = n/nRef
gen lognnorm = log(nnorm)
binscatter2 nnorm hr, by(grp) linetype(connect) ///
  msymbols(o oh d dh) ///
  title("Scaled trip counts by hour of day," ///
  "to/from CCK or not, pre-/post-DTL2") xtitle("Hour") ///
  ytitle("Trip count (rel. grp. mean on Dec 1 2015)") ///
  legend(order(1 "All o/r subzones, pre-DTL2" ///
  2 "All o/r subzones, post-DTL2" ///  
  3 "CCK (GHJ), pre-DTL2" ///
  4 "CCK (GHJ), post-DTL2" ///
  ) rows(2))
graph export "${make_data}/aggNormTripsPerHourCCK2015.png", replace width(1920)
restore

// repeat by direction of travel
preserve
gcollapse (count) n = oa, by(gCCK tripDate hr)
gen byte after = (tripDate >= td(27dec2015))
gen logn = log(n)
gegen grp = group(gCCK after)
binscatter2 logn hr, by(grp) linetype(none)

gegen nRef = mean(n) if tripDate == td(01dec2015), by(gCCK)
fillmissing nRef, by(gCCK)
gen nnorm = n/nRef
gen lognnorm = log(nnorm)
binscatter2 nnorm hr, by(grp) linetype(connect) ///
  msymbols(o oh d dh t th s sh) ///
  title("Scaled trip counts by hour of day by origin & destination," ///
  "in CCK (GHJ), pre-/post-DTL2") ///
  xlabel(#12, labsize(small)) ylabel(, labsize(small)) ///
  xtitle("Hour") ///
  ytitle("Trip count (rel. grp. mean on Dec 1 2015)") ///
  legend(order(1 "Outside CCK, pre-DTL2" ///
  2 "Outside CCK, post-DTL2" ///  
  3 "Starting in CCK, pre-DTL2" ///
  4 "Starting in CCK, post-DTL2" ///
  5 "Ending in CCK, pre-DTL2" ///
  6 "Ending in CCK, post-DTL2" ///
  7 "Within CCK, pre-DTL2" ///
  8 "Within CCK, post-DTL2" ///
  ) rows(4))
graph export "${make_data}/aggNormODTripsPerHourCCK2015.png", replace width(1920)
restore

// repeat by card type

preserve
gcollapse (count) n = oa, by(CCK tripDate hr TKT_TYP_CD)
gen byte after = (tripDate >= td(27dec2015))
gen logn = log(n)
gegen grp = group(CCK TKT_TYP_CD after)
binscatter2 logn hr if TKT_TYP_CD == 34 | TKT_TYP_CD == 38 ///
  , by(grp) linetype(none)
gegen nRef = mean(n) if tripDate == td(01dec2015), by(CCK TKT_TYP_CD)
fillmissing nRef, by(CCK TKT_TYP_CD)
gen nnorm = n/nRef
gen lognnorm = log(nnorm+1)
binscatter2 nnorm hr if TKT_TYP_CD == 34 | TKT_TYP_CD == 38, ///
  by(grp) linetype(connect) ///
  msymbols(p p p p o oh d dh) ///
  title("Scaled trip counts by hour of day by card type," ///
  "to/from CCK (GHJ) or not, pre-/post-DTL2") xtitle("Hour") ///
  ytitle("Trip count (rel. grp. mean on Dec 1 2015)") ///
  legend(order(1 "Adult, o/r, pre-DTL2" ///
  2 "Adult, o/r, post-DTL2" ///  
  3 "Workfare, o/r, pre-DTL2" ///
  4 "Workfare, o/r, post-DTL2" ///
  5 "Adult, CCK, pre-DTL2" ///
  6 "Adult, CCK, post-DTL2" ///  
  7 "Workfare, CCK, pre-DTL2" ///
  8 "Workfare, CCK, post-DTL2" ///
  ) rows(4))
graph export "${make_data}/norm3438TripsPerHourCCK2015.png", replace width(1920)

gcollapse (mean) n, by(hr CCK TKT_TYP_CD after)
greshape wide n, by(CCK TKT_TYP_CD hr) keys(after)
gen ndiff = n1-n0
gen fracdiff = ndiff/n0
gegen grp = group(CCK TKT_TYP_CD)
sepscatter ndiff hr ///
  if (TKT_TYP_CD == 34 | TKT_TYP_CD == 38) & hr >= 5, ///
  separate(grp) recast(connect)
sepscatter fracdiff hr ///
  if (TKT_TYP_CD == 34 | TKT_TYP_CD == 38) & hr >= 5, ///
  separate(grp) recast(connect) ///
  lpattern(solid solid shortdash shortdash) ///
  xlabel(#12, labsize(small)) ylabel(, labsize(small)) ///
  title("Scaled trip count differences by time of day by card type," ///
  "to/from CCK (GHJ) or not, pre-/post-DTL2") ///
  xtitle("Hour") ytitle("Trip count diff. (rel. pre-DTL2)") ///
  legend(order(1 "Adult, o/r subzones" ///
  2 "Workfare, o/r subzones" ///  
  3 "Adult, CCK (GHJ)" ///
  4 "Workfare, CCK (GHJ)" ///
  ) rows(2))
graph export "${make_data}/normDiff3438TripsPerHourCCK2015.png", replace width(1920)
restore

// repeat by card type and direction of travel
preserve
gcollapse (count) n = oa, by(gCCK tripDate hr TKT_TYP_CD)
gen byte after = (tripDate >= td(27dec2015))
/* gen logn = log(n) */
gegen grp = group(gCCK TKT_TYP_CD after)
gegen nRef = mean(n) if tripDate == td(01dec2015), by(gCCK TKT_TYP_CD)
fillmissing nRef, by(gCCK TKT_TYP_CD)
gen nnorm = n/nRef
/* gen lognnorm = log(nnorm) */
foreach ttype in 34 38 40 41 42 {
	qui binscatter nnorm hr if TKT_TYP_CD == `ttype' & hr >= 5, by(grp) linetype(connect) ///
	  msymbols(o oh d dh t th s sh) ///
	  title("Scaled trip counts by hour of day by origin & destination," ///
	  "in CCK (GHJ), pre-/post-DTL2 (Farecard ID: `ttype')") ///
	  xlabel(#12, labsize(small)) ylabel(, labsize(small)) ///
	  xtitle("Hour") ///
	  ytitle("Trip count (rel. grp. mean on Dec 1 2015)") ///
	  legend(order(1 "Outside CCK, pre-DTL2" ///
	  2 "Outside CCK, post-DTL2" ///  
	  3 "Starting in CCK, pre-DTL2" ///
	  4 "Starting in CCK, post-DTL2" ///
	  5 "Ending in CCK, pre-DTL2" ///
	  6 "Ending in CCK, post-DTL2" ///
	  7 "Within CCK, pre-DTL2" ///
	  8 "Within CCK, post-DTL2" ///
	  ) rows(4))
	qui graph export "${make_data}/norm`ttype'ODTripsPerHourCCK2015.png", replace width(1920)
	}

gcollapse (mean) n, by(hr gCCK TKT_TYP_CD after)
greshape wide n, by(gCCK TKT_TYP_CD hr) keys(after)
gen ndiff = n1-n0
gen fracdiff = ndiff/n0
gegen grp = group(gCCK TKT_TYP_CD)
foreach ttype in 34 38 40 41 42 {
	qui sepscatter fracdiff hr ///
	  if (TKT_TYP_CD == `ttype') & hr >= 5, ///
	  separate(grp) recast(connect) ///
	  lpattern(solid solid shortdash shortdash) ///
	  xlabel(#12, labsize(small)) ylabel(, labsize(small)) ///
	  title("Scaled trip count diffs by orig./dest. & time of day," ///
	  "to CCK (GHJ), pre-/post-DTL2 (Farecard ID: `ttype')") ///
	  xtitle("Hour") ytitle("Trip count diff. (rel. pre-DTL2)") ///
	  legend(order(1 "Outside CCK" ///
	  2 "Starting in CCK" ///  
	  3 "Ending in CCK" ///
	  4 "Within CCK" ///
	  ) rows(2))
	qui graph export "${make_data}/normDiff`ttype'ODTripsPerHourCCK2015.png", replace width(1920)
	}
restore

/***************************/
// repeat for beauty world //
/***************************/

fv
frame put oa bw gbw tripDate hr TKT_TYP_CD, into(bwbyhour)
fv bwbyhour
preserve
gcollapse (count) n = oa, by(bw tripDate hr)
gen byte after = (tripDate >= td(27dec2015))
gen logn = log(n)
gegen grp = group(bw after)
gegen nRef = mean(n) if tripDate == td(01dec2015), by(bw)
fillmissing nRef, by(bw)
gen nnorm = n/nRef
gen lognnorm = log(nnorm)
binscatter2 nnorm hr, by(grp) linetype(connect) ///
  msymbols(o oh d dh) ///
  xlabel(#12, labsize(small)) ylabel(, labsize(small)) ///
  title("Scaled trip counts by hour of day," ///
  "to/from Beauty World or not, pre-/post-DTL2") xtitle("Hour") ///
  ytitle("Trip count (rel. grp. mean on Dec 1 2015)") ///
  legend(order(1 "All o/r subzones, pre-DTL2" ///
  2 "All o/r subzones, post-DTL2" ///  
  3 "Beauty World, pre-DTL2" ///
  4 "Beauty World, post-DTL2" ///
  ) rows(2))
graph export "${make_data}/aggNormTripsPerHourBW2015.png", replace width(1920)
restore

// beauty world: direction of travel
preserve
gcollapse (count) n = oa, by(gbw tripDate hr)
gen byte after = (tripDate >= td(27dec2015))
gen logn = log(n)
gegen grp = group(gbw after)
binscatter2 logn hr, by(grp) linetype(none)

gegen nRef = mean(n) if tripDate == td(01dec2015), by(gbw)
fillmissing nRef, by(gbw)
gen nnorm = n/nRef
gen lognnorm = log(nnorm)
binscatter2 nnorm hr, by(grp) linetype(connect) ///
  msymbols(o oh d dh t th s sh) ///
  title("Scaled trip counts by hour of day by origin & destination," ///
   "in Beauty World, pre-/post-DTL2") ///
  xtitle("Hour") ///
  ytitle("Trip count (rel. grp. mean on Dec 1 2015)") ///
  legend(order(1 "Outside BW, pre-DTL2" ///
  2 "Outside BW, post-DTL2" ///  
  3 "Starting in BW, pre-DTL2" ///
  4 "Starting in BW, post-DTL2" ///
  5 "Ending in BW, pre-DTL2" ///
  6 "Ending in BW, post-DTL2" ///
  7 "Within BW, pre-DTL2" ///
  8 "Within BW, post-DTL2" ///
  ) rows(4))
graph export "${make_data}/aggNormODTripsPerHourBW2015.png", replace width(1920)
restore

// beauty world: by card type
preserve
gcollapse (count) n = oa, by(bw tripDate hr TKT_TYP_CD)
gen byte after = (tripDate >= td(27dec2015))
gen logn = log(n)
gegen grp = group(bw TKT_TYP_CD after)
gegen nRef = mean(n) if tripDate == td(01dec2015), by(bw TKT_TYP_CD)
fillmissing nRef, by(bw TKT_TYP_CD)
gen nnorm = n/nRef
gen lognnorm = log(nnorm+1)
binscatter nnorm hr if TKT_TYP_CD == 34 | TKT_TYP_CD == 38, ///
  by(grp) linetype(connect) ///
  msymbols(p p p p o oh d dh) ///
  title("Scaled trip counts by hour of day by card type," ///
  "to/from Beauty World or not, pre-/post-DTL2") xtitle("Hour") ///
  ytitle("Trip count (rel. grp. mean on Dec 1 2015)") ///
  legend(order(1 "Adult, o/r, pre-DTL2" ///
  2 "Adult, o/r, post-DTL2" ///  
  3 "Workfare, o/r, pre-DTL2" ///
  4 "Workfare, o/r, post-DTL2" ///
  5 "Adult, BW, pre-DTL2" ///
  6 "Adult, BW, post-DTL2" ///  
  7 "Workfare, BW, pre-DTL2" ///
  8 "Workfare, BW, post-DTL2" ///
  ) rows(4))
graph export "${make_data}/norm3438TripsPerHourBW2015.png", replace width(1920)

gcollapse (mean) n, by(hr bw TKT_TYP_CD after)
greshape wide n, by(bw TKT_TYP_CD hr) keys(after)
gen ndiff = n1-n0
gen fracdiff = ndiff/n0
gegen grp = group(bw TKT_TYP_CD)
sepscatter ndiff hr ///
  if (TKT_TYP_CD == 34 | TKT_TYP_CD == 38) & hr >= 5, ///
  separate(grp) recast(connect)
sepscatter fracdiff hr ///
  if (TKT_TYP_CD == 34 | TKT_TYP_CD == 38) & hr >= 5, ///
  separate(grp) recast(connect) ///
  lpattern(solid solid shortdash shortdash) ///
  xlabel(#12, labsize(small)) ylabel(, labsize(small)) ///
  title("Scaled trip count differences by time of day by card type," ///
  "to/from Beauty World or not, pre-/post-DTL2") ///
  xtitle("Hour") ytitle("Trip count diff. (rel. pre-DTL2)") ///
  legend(order(1 "Adult, o/r subzones" ///
  2 "Workfare, o/r subzones" ///  
  3 "Adult, BW" ///
  4 "Workfare, BW" ///
  ) rows(2))
graph export "${make_data}/normDiff3438TripsPerHourBW2015.png", replace width(1920)
restore

// beauty world: by card type and direction of travel
preserve
gcollapse (count) n = oa, by(gbw tripDate hr TKT_TYP_CD)
gen byte after = (tripDate >= td(27dec2015))
gen logn = log(n)
gegen grp = group(gbw TKT_TYP_CD after)
gegen nRef = mean(n) if tripDate == td(01dec2015), by(gbw TKT_TYP_CD)
fillmissing nRef, by(gbw TKT_TYP_CD)
gen nnorm = n/nRef
gen lognnorm = log(nnorm)
foreach ttype in 34 38 40 41 42 {
	qui binscatter nnorm hr if TKT_TYP_CD == `ttype' & hr >= 5, by(grp) linetype(connect) ///
	  msymbols(o oh d dh t th s sh) ///
	  title("Scaled trip counts by hour of day by origin & destination," ///
	  "in Beauty World, pre-/post-DTL2 (Farecard ID: `ttype')") ///
	  xlabel(#12, labsize(small)) ylabel(, labsize(small)) ///
	  xtitle("Hour") ///
	  ytitle("Trip count (rel. grp. mean on Dec 1 2015)") ///
	  legend(order(1 "Outside BW, pre-DTL2" ///
	  2 "Outside BW, post-DTL2" ///  
	  3 "Starting in BW, pre-DTL2" ///
	  4 "Starting in BW, post-DTL2" ///
	  5 "Ending in BW, pre-DTL2" ///
	  6 "Ending in BW, post-DTL2" ///
	  7 "Within BW, pre-DTL2" ///
	  8 "Within BW, post-DTL2" ///
	  ) rows(4))
	qui graph export "${make_data}/norm`ttype'ODTripsPerHourBW2015.png", replace width(1920)
	}

gcollapse (mean) n, by(hr gbw TKT_TYP_CD after)
greshape wide n, by(gbw TKT_TYP_CD hr) keys(after)
gen ndiff = n1-n0
gen fracdiff = ndiff/n0
gegen grp = group(gbw TKT_TYP_CD)
foreach ttype in 34 38 40 41 42 {
	qui sepscatter fracdiff hr ///
	  if (TKT_TYP_CD == `ttype') & hr >= 5, ///
	  separate(grp) recast(connect) ///
	  lpattern(solid solid shortdash shortdash) ///
	  xlabel(#12, labsize(small)) ylabel(, labsize(small)) ///
	  title("Scaled trip count diffs by orig./dest. & time of day," ///
	  "to Beauty World, pre-/post-DTL2 (Farecard ID: `ttype')") ///
	  xtitle("Hour") ytitle("Trip count diff. (rel. pre-DTL2)") ///
	  legend(order(1 "Outside BW" ///
	  2 "Starting in BW" ///  
	  3 "Ending in BW" ///
	  4 "Within BW" ///
	  ) rows(2))
	qui graph export "${make_data}/normDiff`ttype'ODTripsPerHourBW2015.png", replace width(1920)
	}
restore

// repeat for downtown

fv
frame put oa dt gdt tripDate hr TKT_TYP_CD, into(dtbyhour)
fv dtbyhour
preserve
gcollapse (count) n = oa, by(dt tripDate hr)
gen byte after = (tripDate >= td(27dec2015))
gen logn = log(n)
gegen grp = group(dt after)
gegen nRef = mean(n) if tripDate == td(01dec2015), by(dt)
fillmissing nRef, by(dt)
gen nnorm = n/nRef
gen lognnorm = log(nnorm)
binscatter2 nnorm hr, by(grp) linetype(connect) ///
  msymbols(o oh d dh) ///
  xlabel(#12, labsize(small)) ylabel(, labsize(small)) ///
  title("Scaled trip counts by hour of day," ///
  "to/from Downtown or not, pre-/post-DTL2") xtitle("Hour") ///
  ytitle("Trip count (rel. grp. mean on Dec 1 2015)") ///
  legend(order(1 "All o/r subzones, pre-DTL2" ///
  2 "All o/r subzones, post-DTL2" ///  
  3 "Downtown, pre-DTL2" ///
  4 "Downtown, post-DTL2" ///
  ) rows(2))
graph export "${make_data}/aggNormTripsPerHourDT2015.png", replace width(1920)
restore

preserve
gcollapse (count) n = oa, by(dt tripDate hr TKT_TYP_CD)
gen byte after = (tripDate >= td(27dec2015))
gen logn = log(n)
gegen grp = group(dt TKT_TYP_CD after)
gegen nRef = mean(n) if tripDate == td(01dec2015), by(dt TKT_TYP_CD)
fillmissing nRef, by(dt TKT_TYP_CD)
gen nnorm = n/nRef
gen lognnorm = log(nnorm+1)
binscatter nnorm hr if TKT_TYP_CD == 34 | TKT_TYP_CD == 38, ///
  by(grp) linetype(connect) ///
  msymbols(p p p p o oh d dh) ///
  title("Scaled trip counts by hour of day by card type," ///
  "to/from Downtown or not, pre-/post-DTL2") xtitle("Hour") ///
  ytitle("Trip count (rel. grp. mean on Dec 1 2015)") ///
  legend(order(1 "Adult, o/r, pre-DTL2" ///
  2 "Adult, o/r, post-DTL2" ///  
  3 "Workfare, o/r, pre-DTL2" ///
  4 "Workfare, o/r, post-DTL2" ///
  5 "Adult, DT, pre-DTL2" ///
  6 "Adult, DT, post-DTL2" ///  
  7 "Workfare, DT, pre-DTL2" ///
  8 "Workfare, DT, post-DTL2" ///
  ) rows(4))
graph export "${make_data}/norm3438TripsPerHourDT2015.png", replace width(1920)

gcollapse (mean) n, by(hr dt TKT_TYP_CD after)
greshape wide n, by(dt TKT_TYP_CD hr) keys(after)
gen ndiff = n1-n0
gen fracdiff = ndiff/n0
gegen grp = group(dt TKT_TYP_CD)
sepscatter ndiff hr ///
  if (TKT_TYP_CD == 34 | TKT_TYP_CD == 38) & hr >= 5, ///
  separate(grp) recast(connect)
sepscatter fracdiff hr ///
  if (TKT_TYP_CD == 34 | TKT_TYP_CD == 38) & hr >= 5, ///
  separate(grp) recast(connect) ///
  lpattern(solid solid shortdash shortdash) ///
  xlabel(#12, labsize(small)) ylabel(, labsize(small)) ///
  title("Scaled trip count differences by time of day by card type," ///
  "to/from Downtown or not, pre-/post-DTL2") ///
  xtitle("Hour") ytitle("Trip count diff. (rel. pre-DTL2)") ///
  legend(order(1 "Adult, o/r subzones" ///
  2 "Workfare, o/r subzones" ///
  3 "Adult, DT" ///
  4 "Workfare, DT" ///
  ) rows(2))
graph export "${make_data}/normDiff3438TripsPerHourDT2015.png", replace width(1920)
restore

// downtown: by card type and direction of travel
preserve
gcollapse (count) n = oa, by(gdt tripDate hr TKT_TYP_CD)
gen byte after = (tripDate >= td(27dec2015))
gen logn = log(n)
gegen grp = group(gdt TKT_TYP_CD after)
gegen nRef = mean(n) if tripDate == td(01dec2015), by(gdt TKT_TYP_CD)
fillmissing nRef, by(gdt TKT_TYP_CD)
gen nnorm = n/nRef
gen lognnorm = log(nnorm)
foreach ttype in 34 38 42 {
	qui binscatter nnorm hr if TKT_TYP_CD == `ttype' & hr >= 5, by(grp) linetype(connect) ///
	  msymbols(o oh d dh t th s sh) ///
	  title("Scaled trip counts by hour of day by origin & destination," ///
	  "Downtown, pre-/post-DTL2 (Farecard ID: `ttype')") ///
	  xlabel(#12, labsize(small)) ylabel(, labsize(small)) ///
	  xtitle("Hour") ///
	  ytitle("Trip count (rel. grp. mean on Dec 1 2015)") ///
	  legend(order(1 "Outside DT, pre-DTL2" ///
	  2 "Outside DT, post-DTL2" ///  
	  3 "Starting in DT, pre-DTL2" ///
	  4 "Starting in DT, post-DTL2" ///
	  5 "Ending in DT, pre-DTL2" ///
	  6 "Ending in DT, post-DTL2" ///
	  7 "Within DT, pre-DTL2" ///
	  8 "Within DT, post-DTL2" ///
	  ) rows(4))
	qui graph export "${make_data}/norm`ttype'ODTripsPerHourDT2015.png", replace width(1920)
	}

gcollapse (mean) n, by(hr gdt TKT_TYP_CD after)
greshape wide n, by(gdt TKT_TYP_CD hr) keys(after)
gen ndiff = n1-n0
gen fracdiff = ndiff/n0
gegen grp = group(gdt TKT_TYP_CD)
foreach ttype in 34 38 42 {
	qui sepscatter fracdiff hr ///
	  if (TKT_TYP_CD == `ttype') & hr >= 5, ///
	  separate(grp) recast(connect) ///
	  lpattern(solid solid shortdash shortdash) ///
	  xlabel(#12, labsize(small)) ylabel(, labsize(small)) ///
	  title("Scaled trip count diffs by orig./dest. & time of day," ///
	  "Downtown, pre-/post-DTL2 (Farecard ID: `ttype')") ///
	  xtitle("Hour") ytitle("Trip count diff. (rel. pre-DTL2)") ///
	  legend(order(1 "Outside DT" ///
	  2 "Starting in DT" ///  
	  3 "Ending in DT" ///
	  4 "Within DT" ///
	  ) rows(2))
	qui graph export "${make_data}/normDiff`ttype'ODTripsPerHourDT2015.png", replace width(1920)
	}
restore
